package com.hx.hospital.service.impl;

import com.hx.hospital.mapper.ClinicCardMapper;
import com.hx.hospital.pojo.ClinicCard;
import com.hx.hospital.service.ClinicCardService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ClinicCardServiceImpl implements ClinicCardService {

    @Autowired
    private ClinicCardMapper clinicCardMapper;

    @Override
    public void insert(ClinicCard clinicCard) {
        clinicCardMapper.insert(clinicCard);
    }

    @Override
    public void updateClinicCard(ClinicCard clinicCard) {
        clinicCardMapper.delete(clinicCard);
        clinicCardMapper.insert(clinicCard);
    }

    @Override
    public void deleteClinicCard(Long id) {
        clinicCardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateClinicCardByRelationship(ClinicCard clinicCard) {

        //查询所有
        Iterable<ClinicCard> all = clinicCardMapper.selectAll();
        for (ClinicCard c:all){
            if (c.getUserId()==clinicCard.getUserId() && c.getDefaultPeople()==1){
                //删除默认
                clinicCardMapper.delete(c);
                c.setDefaultPeople(0);
                //保存
                clinicCardMapper.insert(c);
            }
        }
        clinicCardMapper.selectOne(clinicCard);
        clinicCardMapper.insert(clinicCard);
    }

    @Override
    public List<ClinicCard> queryByUserId(Long id) {
        List<ClinicCard> clinicCards = clinicCardMapper.queryByUserId(id);
        return clinicCards;
    }


}

